import sys
import json
import requests

user_input = sys.argv[1]

request_data = {
    "model": "deepseek-ai/DeepSeek-R1",
    "messages": [
        {"role": "system", "content": "你用户的朋友"},
        {"role": "user", "content": user_input}
    ],
    "stream": True
}

headers = {
    "accept": "application/json",
    "content-type": "application/json",
    "authorization": "Bearer ***"
}

try:
    response = requests.post(
        "https://api.siliconflow.cn/v1/chat/completions",
        headers=headers,
        json=request_data,
        stream=True
    )
    response.raise_for_status()
    for line in response.iter_lines():
        if line:
            decoded_line = line.decode('utf-8')
            if decoded_line.startswith("data: "):
                json_data = decoded_line[6:]
                try:
                    data = json.loads(json_data)
                    delta = data["choices"][0]["delta"]
                    content = delta.get("content", None)
                    reasoning_content = delta.get("reasoning_content", "")
                    if reasoning_content:
                        for char in reasoning_content:
                            print(char, end='', flush=True)
                    if content :
                        for char in content:
                            print(char, end='', flush=True)
                except json.JSONDecodeError:
                    pass
except requests.RequestException as e:
    print(f"Request error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
